Standard Datatypes

ION supports many of PHP's data types:

Nulls

Additonally to the plain and simple NULL, ION can attach a type to NULL values.


<?php
  
$writer 
= new ion\Writer\Stream\Writer(STDOUT);
$writer->writeNull();
$writer->writeTypedNull(ion\Type::Int);
$writer->writeTypedNull(ion\Type::String);
$writer->flush();

/*
    null
    null.int
    null.string
*/

?>

Booleans

The bool type does not need a lot of explanation:


<?php

$writer
->writeBool(true);
$writer->writeBool(false);
$writer->flush();

/*
    true
    false
*/

?>

Integers

The int type comprises signed integers of arbitrary size.


<?php
  
$writer
->writeInt(123);
$writer->writeInt("12345678901234567890");
$writer->flush();

/*
    123
    12345678901234567890
*/

?>

In ION-Text, underscores are allowed to separate digits:


<?php
  
$reader 
= new ion\Reader\Buffer\Reader("-123_456_789");
$reader->next();
var_dump($reader->getType());
var_dump($reader->readInt());

/*
    enum(ion\Type::Int)
    int(-123456789)
*/

?>

Hexadecimal as well as binary notation are supported, too:


<?php

$reader 
= new ion\Reader\Buffer\Reader("0xdead_beef");
$reader->next();
var_dump($reader->readInt());

/*
    int(3735928559)
*/

$reader = new ion\Reader\Buffer\Reader("0b10000100_11001001");
$reader->next();
var_dump($reader->readInt());

/*
    int(33993)
*/

?>

Reals

Ion supports both binary and lossless decimal encodings of real numbers as, respectively, types float and decimal. In the text format, float values are denoted much like the decimal formats in C or Java; decimal values use d instead of e to start the exponent.

Reals without an exponent are treated as decimal. As with JSON, extra leading zeros are not allowed. Digits may be separated with an underscore.

Floats


<?php

var_dump
(ion\serialize(0.123));

/*
    string(25) "0.12299999999999999822e+0"
*/

var_dump(ion\unserialize("[0.123e, 123e-3]"));

/*
  array(2) {
    [0]=>
    float(0.123)
    [1]=>
    float(0.123)
  }
*/

?>

Decimals


<?php

var_dump
(ion\serialize(new ion\Decimal("0.123")));

/*
    string(5) "0.123"
*/

var_dump(ion\unserialize("[0.123d0, 123d-3]"));

/*
  array(2) {
    [0]=>
    object(ion\Decimal)#8 (2) {
      ["number"]=>
      string(5) "0.123"
      // ...
    }
    [1]=>
    object(ion\Decimal)#11 (2) {
      ["number"]=>
      string(5) "0.123"
      // ...
    }
  }
*/

?>

Strings

Ion strings are Unicode character sequences of arbitrary length.

In the text format, strings are delimited by double-quotes and follow common backslash-escape conventions (see official spec). The binary format always uses UTF-8 encoding.


<?=

ion\serialize
([
  
"abc""new
line"
]);

/*
    ["abc", "new\nline"]
*/

?>

Long Strings

The text format supports an alternate syntax for “long strings”, including those that break across lines. Sequences bounded by three single-quotes (''') can cross multiple lines and still count as a valid, single string. In addition, any number of adjacent triple-quoted strings are concatenated into a single value.

The concatenation happens within the Ion text parser and is neither detectable via the data model nor applicable to the binary format. Note that comments are always treated as whitespace, so concatenation still occurs when a comment falls between two long strings.


<?php
  
var_dump
(ion\unserialize("
'''
  here are 
  several new
  lines
'''
"
));

/*
string(35) "
  here are 
  several new
  lines
"
*/

?>

Containers

Ion defines three container types: structures, lists, and S-expressions. These types are defined recursively and may contain values of any Ion type.

Lists

Lists are ordered collections of values. The contents of the list are heterogeneous (that is, each element can have a different type). In the text format, lists are bounded by square brackets and elements are separated by commas.


<?=

ion\serialize
([1"yes"null]);

/*
  [1,"yes",null]
*/

?>

Structures

Structures are unordered collections of name/value pairs. The names are symbol tokens, and the values are unrestricted. Each name/value pair is called a field.

In the text format, structures are wrapped by curly braces, with a colon between each name and value, and a comma between the fields. For the purposes of JSON compatibility, it’s also legal to use strings for field names, but they are converted to symbol tokens by the parser.


<?=

ion\serialize
([
  
"outlaw",
  
"key" => "value",
  
"obj" => (object)["key" => "value"]
]);

/*
    {'0':"outlaw",key:"value",obj:o::{key:"value"}}
*/

?>

<?php

var_dump
(ion\unserialize(
  
'{\'0\':"outlaw",key:"value",obj:o::{key:"value"}}'
));

/*
  array(3) {
    [0]=>
    string(6) "outlaw"
    ["key"]=>
    string(5) "value"
    ["obj"]=>
    object(stdClass)#10 (1) {
      ["key"]=>
      string(5) "value"
    }
  }
*/

?>

Next up